﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using KinectXNATools;
using JupiterLibrary;
namespace KinnectInput.KinectMetro
{
    public class KinectMetroButtonGrp:IClicableItem
    {
        Rectangle _groupArea;
        public Rectangle Bounds{
            get
            {
                Point p = XNAMathHelper.ToPoint(Parent.Position + this.Position);
                return new Rectangle(p.X, p.Y,_groupArea.Width,_groupArea.Height);
            }
        }
        public Sprite backGroundText { get; private set; }
        public bool isBackgroundVisible;
        public Vector2 Position { get; private set; }
        public KinectMetroCardContainer Parent{ get; private set;}
        GraphicsDevice _graphicsDevice;
        public bool wasContined { set; get; }
        public List<KinectMetroBtn> Buttons { get; private set; }

        public KinectMetroButtonGrp(Rectangle area, Vector2 position, GraphicsDevice graphicsDevice)
        {
            //esto es debug
            isBackgroundVisible = true;

            this._groupArea = area;
            this.Position = position;
            this._graphicsDevice = graphicsDevice;
            Buttons = new List<KinectMetroBtn>();
            wasContined = false;
        }

        public KinectMetroButtonGrp(Rectangle area, Vector2 position)
        {
            //esto es debug
            isBackgroundVisible = true;

            this._groupArea = area;
            this.Position = position;
            Buttons = new List<KinectMetroBtn>();
        }

        public void BuildGroup(KinectMetroCardContainer parent)
        {
            this.Parent = parent;
            backGroundText = new Sprite();
            backGroundText.SetRectangle(Bounds);
            backGroundText.Texture = Texture2DHelper.TextureFromColor(_graphicsDevice, Bounds, Color.Lime);
            backGroundText.Update();
        }

        public void BuildGroup(KinectMetroCardContainer parent, Texture2D backgroundTexture)
        {
            this.Parent = parent;
            backGroundText = new Sprite();
            backGroundText.SetRectangle(Bounds);
            backGroundText.Texture = backgroundTexture;
            backGroundText.Update();
            
        }
        public event ClickEventHandler Pushed;

        public event ReleasedEventHandler Release;

        public event ItemAreaEnterHandler EnterArea;

        public event ItemAreaLeaveHandler LeaveArea;

        public virtual void OnEnterArea(EnterAreaEventArgs e)
        {
            if (EnterArea != null)
            {
                EnterArea(this, e);
            }
        }

        public virtual void OnLeveArea(EventArgs e)
        {
            if (LeaveArea != null)
            {
                LeaveArea(this, e);
            }
        }

        protected virtual void OnPushed(ProximityClickEventArgs e)
        {
            
            if (Pushed != null)
            {
                Pushed(this, e);
                Mouse.SetPosition(0, 0);
            }
            //si se da un push, Resetear
            Mouse.SetPosition(0,0);
        }
        public virtual void OnReleased(EventArgs e)
        {
            if (Release != null)
            {
                Release(this, e);
            }
        }

        public void Update()
        {
            Vector2 realPos = this.Parent.Position + this.Position;
            this._groupArea.X = (int)realPos.X;
            this._groupArea.Y = (int)realPos.Y;
            backGroundText.SetRectangle(this.Bounds);
            backGroundText.Update();
            foreach (KinectMetroBtn kmib in this.Buttons)
            {
                kmib.Update();
            }
            Point p=new Point(Mouse.GetState().X, Mouse.GetState().Y);
            if (p != Point.Zero)
            {
                Console.Write("");
            }
            if(Contains(Bounds,p))
            {
                OnPushed(new ProximityClickEventArgs());
            }
        }

        public bool Contains(Rectangle area, Point p)
        {
            return area.Contains(p);
        }

        public void Draw()
        {
            
            //backGroundText.Draw();
            if (this.Buttons == null || this.Buttons.Count == 0)
            {
                backGroundText.Draw();
            }
            else
            {
                foreach (KinectMetroBtn kmib in this.Buttons)
                {
                    kmib.Draw();
                }
            }
        }
        public void addButton(KinectMetroBtn kb)
        {
            switch (Buttons.Count)
            {
                case 0:
                    kb.Position = Vector2.Zero;
                    break;
                case 1:
                    if (Buttons[0].Bounds.Width < MenuItemSizes.LongHorizontalButton.Width)
                    {
                        kb.Position = new Vector2(MenuItemSizes.separation + Buttons[0].Bounds.Width,0);
                    }
                    else
                    {
                        kb.Position = new Vector2(0,MenuItemSizes.separation + Buttons[0].Bounds.Height);
                    }
                    break;
                case 2:
                    if (Buttons[0].Bounds.Width < MenuItemSizes.LongHorizontalButton.Width)
                    {
                        if (Buttons[1].Bounds.Width < MenuItemSizes.HorizontalCardContainer.Width)
                        {
                            kb.Position = new Vector2(0,MenuItemSizes.separation + Buttons[0].Bounds.Height);
                        }
                        //esto es un equivocacion al montar el menu
                        else
                        {
                            Vector2 vec = new Vector2(0,MenuItemSizes.separation + Buttons[0].Bounds.Height);
                            kb.Position = vec * 2;
                        }
                    }
                    else
                    {
                        if (Buttons[1].Bounds.Width < MenuItemSizes.LongHorizontalButton.Width)
                        {
                            kb.Position = new Vector2(MenuItemSizes.separation + Buttons[1].Bounds.Width, MenuItemSizes.separation + Buttons[0].Bounds.Height);
                        }
                        //esto es un equivocacion al montar el menu
                        else
                        {
                            Vector2 vec = new Vector2(0, MenuItemSizes.separation + Buttons[0].Bounds.Height);
                            kb.Position = vec * 2;
                        }
                    }
                    break;
                case 3:
                    if (Buttons[0].Bounds.Width < MenuItemSizes.LongHorizontalButton.Width && Buttons[1].Bounds.Width < MenuItemSizes.LongHorizontalButton.Width && Buttons[2].Bounds.Width < MenuItemSizes.LongHorizontalButton.Width)
                    {
                        Vector2 vec = new Vector2(MenuItemSizes.separation + Buttons[0].Bounds.Width, MenuItemSizes.separation + Buttons[0].Bounds.Height);
                        kb.Position = vec;
                    }
                    break;
            }
            Buttons.Add(kb);
        }
        public void addSingleButton(KinectMetroBtn kb)
        {
            //si añades un solo boton, lo centras y fuera
            kb.Position = new Vector2((this._groupArea.Width / 2) - (kb.Bounds.Width / 2), (this._groupArea.Height / 2) - (kb.Bounds.Height / 2));
            Buttons.Add(kb);
        }
    }
}
